package com.buddy.java.thread.common;

/**
 * 告警功能入口类
 * 模式角色:Two-phaseTermination.ThreadOver
 */
public class AlarmMgr {

    private static final AlarmMgr INSTANCE = new AlarmMgr();

    private volatile boolean shutdownRequest = false;

    //告警发送线程
    private final AlarmSendingThread alarmSendingThread;

    //私有构造器
    private AlarmMgr(){
        alarmSendingThread = new AlarmSendingThread();
    }

    //返回类AlramMgr的唯一实例
    public static AlarmMgr getInstance(){
        return INSTANCE;
    }

    /**
     * 发送告警
     * @param type 告警类型
     * @param id 告警编号
     * @param extraInfo 告警参数
     * @return 由type+id+extraInfo唯一确定的告警信息被提交的次数,-1表示告警管理器被关闭
     */
    public int sendAlarm(AlarmType type, String id, String extraInfo){
        System.out.println("Trigger alarm"+type+","+id+","+extraInfo);
        int duplicateSubmissionCount = 0;
        AlarmInfo alarmInfo = new AlarmInfo(id,type);
        alarmInfo.setExtraInfo(extraInfo);
        duplicateSubmissionCount = alarmSendingThread.sendAlarm(alarmInfo);

        return duplicateSubmissionCount;
    }
}
